package org.cttic.visualization.visualization.z_service.first;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.z_service.stationCountAllQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-27 10:16
 * 2.1.2收费站出入汇总统计查询
 */
@Service
public class stationCountAllQueryImpl implements stationCountAllQuery {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public String stationCountQuery(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String stationId = jsonData.getString("stationId");         //收费站ID
        String vehStatus = jsonData.getString("vehStatus");        //稽查状态
        String topic = jsonData.getString("topic");                 //通行主题
        String queryType = jsonData.getString("queryType");         //0:按系统当前时间点查询  1:按客户端请求时间段查询
        String queryValue1 = jsonData.getString("queryValue1");
        //查询全国
        if(provinceId==null){
            String querySql="SELECT sum(enCount) as enCount,sum(exCount) as exCount,sum(totalCount) as totalCount FROM `stationCount` ";
            System.out.println(querySql);
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
            if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);
                Long enCount = jsonObject.getLong("enCount");
                int enCounts = enCount.intValue();//入站
                Long exCount = jsonObject.getLong("exCount");
                int exCounts = exCount.intValue();//出站
                Long totalCount = jsonObject.getLong("totalCount");
                int totalCountValue = totalCount.intValue();//出入站总和
                JSONObject json = new JSONObject();
                json.put("enCounts",enCounts);
                json.put("exCounts",exCounts);
                json.put("totalCount",totalCountValue);

                System.out.println("*************查询全国出入站数量信息："+JSONUtil.toJsonStr(json)+"**************");
                return JSONUtil.toJsonStr(json);
            }
            //单省
        }else if(!provinceId.contains("|")){
            String querySql="SELECT sum(enCount) as enCount,sum(exCount) as exCount,sum(totalCount) as totalCount FROM `stationCount`  WHERE provinceId= '"+ provinceId + "'";
            System.out.println(querySql);
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
            if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);
                Long enCount = jsonObject.getLong("enCount");
                int enCounts = enCount.intValue();//入站
                Long exCount = jsonObject.getLong("exCount");
                int exCounts = exCount.intValue();//出站
                Long totalCount = jsonObject.getLong("totalCount");
                int totalCountValue = totalCount.intValue();//出入站总和
                JSONObject json = new JSONObject();
                json.put("enCounts",enCounts);
                json.put("exCounts",exCounts);
                json.put("totalCount",totalCountValue);

                System.out.println("*************查询"+ provinceData.provinceID.get(Integer.parseInt(provinceId))+"出入站数量信息："+JSONUtil.toJsonStr(json)+"**************");
                return JSONUtil.toJsonStr(json);
            }

            //多省
        }else if(provinceId.contains("|")){
            List<JSON> list = new ArrayList<>();
            String[] split = provinceId.split("\\|");
            for (String province : split) {
                String querySql="SELECT sum(enCount) as enCount,sum(exCount) as exCount,sum(totalCount) as totalCount FROM `stationCount`  WHERE provinceId= '"+ province + "'";
                System.out.println(querySql);
                Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
                if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                    cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);

                    Long enCount = jsonObject.getLong("enCount");
                    int enCounts = enCount.intValue();//入站
                    Long exCount = jsonObject.getLong("exCount");
                    int exCounts = exCount.intValue();//出站
                    Long totalCount = jsonObject.getLong("totalCount");
                    int totalCountValue = totalCount.intValue();//出入站总和
                    JSONObject json = new JSONObject();
                    json.put("provinceId",province);
                    json.put("enCounts",enCounts);
                    json.put("exCounts",exCounts);
                    json.put("totalCount",totalCountValue);
                    list.add(json);
                    System.out.println("*************查询"+ provinceData.provinceID.get(Integer.parseInt(province))+"出入站数量信息："+JSONUtil.toJsonStr(json)+"**************");
                }
            }
            return list.toString();
        }
        return null;
    }
}
